﻿using SiteApp.BLL.Common;
using SiteApp.BLL.Interface;
using SiteApp.DAL;
using SiteApp.Model;
using System.Linq;

namespace SiteApp.BLL
{
    public class UserService: IUserService
    {
        public  User SelectUser(string name, string pwd)
        {
          
            pwd = MD5Helper.GenerateMD5(pwd);
          
            var data = SqlDBHelper.Query<User>($"select   * from [User]  where   [Name]='{name}' and [Password]='{pwd}' and Isenable=1 ").FirstOrDefault();
           
            data.Password = MD5Helper.GenerateMD5(data.Password);

            return data;
        }

       

        public int UpdateIsenable(string userId, int Isenable)
        {
            var result = -1;
            var data = SqlDBHelper.Query<User>($"select   * from [User]  where   [SID]='{userId}' ").FirstOrDefault();

            if (data != null)
            {
                data.Isenable = Isenable;
                result = SqlDBHelper.Execute($"update [User] set Isenable=@Isenable where SID=@SID", data);
            }
            return result;

        }

        public int UpdatePassword(string userId, string oldPassword, string newPassword)
        {
            var result = -1;

            oldPassword = MD5Helper.GenerateMD5(oldPassword);
            newPassword = MD5Helper.GenerateMD5(newPassword);

            var data = SqlDBHelper.Query<User>($"select   * from [User]  where   [SID]='{userId}' and [Password]='{oldPassword}' ").FirstOrDefault();

            if (data != null)
            {
                data.Password = newPassword;
                result = SqlDBHelper.Execute($"update [User] set Password=@Password where SID=@SID", data);
            }
            return result;

        }




    }

}
